REMOTE_ENABLED	:= 0

SM_version := sm_11

#############################
# G95 compiler              #
#############################

#FC = /usr/local/bin/gfortran
FC=g95
FFLAGS= -cpp -m32 -O3 --no-second-underscore
###############################################################################
## setting up CUDA variables												  #	
###############################################################################

#no extension specified
CUDA_FILES_CPP	:= MatTest MatTest_helper IGGS_CUDA_helper MatTest_CPU_helper MatTest_CUBLAS_helper
				
#no extension specified
CUDA_FILES_C	:= errlib msglib

#no extension specified
CUDA_FILES_KERNELS	:= MatTest_kernel

#.cu files
CUFILES_CPP	:= $(CUDA_FILES_CPP:=.cu)
CUFILES_C	:= $(CUDA_FILES_C:=.cu)

#.o files
CUDA_OBJ	:= $(CUDA_FILES_CPP:=.o) $(CUDA_FILES_C:=.o)

#Fortran files
FORTRAN_FILES_F90	:= main

#Fortran .f90 files
FORTRAN_FILES	:= $(FORTRAN_FILES_F90:=.f90)

#Fortran .o files
FORTRAN_OBJ	:= $(FORTRAN_FILES_F90:=.o)

# Additional libraries/settings needed by the project
USECUBLAS	:= 1
USEDRVAPI	:= 1
USECUDPP	:= 0
fastmath	:= 1
SM_xx		:= --gpu-architecture=$(SM_version)

##Notes:
#arch = sm_11 (Compute 1.1) sm_12/sm_13 (Compute 1.2/1.3 --> GT200)
#Driver API and Runtime API can be mixed to a certain extent...

SILENT		:=
SILENT_CONSOLE	:= 
CUDA_LIBS := -lcudart
OUT_CONSOLE	:= > /dev/console
CUDA_NVCC_FLAGS	:=

## To add misc flags for NVCC use the following statement... 
#CUDA_NVCC_FLAGS	+=  

###############################################################################
## Importing external Makefile												  #	
## (subset of the NVIDIA SDK provided common.mk makefile) 					  #
###############################################################################

include nvMake.mk

#some variables must be modified after nvMake.mk has been imported here
LIB := $(LIB) -lstdc++-static 

TARGET	:= FortranMatrixTest
CLI	:= 

MY_CFLAGS := $(MY_CFLAGS) -g -O3 -m32

###############################################################################

default:
	make clean
	make update
	make $(TARGET) "VERBOSE :="
	make cleanup
	
log:
	make clean
	make update
	make $(TARGET) "VERBOSE :=" "CLI := >> logs/make.log 2>&1" >> logs/make.log 2>&1
	make cleanup

dbg:
ifeq ($(REMOTE_ENABLED),1)
	$(SSH_CONNECT_DBG)
else
	make "dbg:=1"
endif
	
debug_log:
	make clean
	make update
	make $(TARGET) "VERBOSE :=" "dbg := 1" "CLI := >> logs/make.log 2>&1" >> logs/make.log 2>&1
	make cleanup

all:
ifeq ($(REMOTE_ENABLED),1)
	$(SSH_CONNECT)
else
	make
endif

all_silent:
	@ echo
	@ echo "Building the project..."
	@ echo 
	@ echo "Build steps: gcc --> nvcc --> g95"
	@ echo
	@ make clean "SILENT_CONSOLE := > /dev/null" > /dev/null
	@ make update "SILENT_CONSOLE := > /dev/null" > /dev/null
	@ make $(TARGET) "SILENT_CONSOLE := > /dev/null" > /dev/null
	@ make cleanup "SILENT_CONSOLE := > /dev/null" > /dev/null
	@ echo
	@ echo "Project built, to launch the program type: ./"$(TARGET)
	@ echo

##device emulation mode target (to be completed...)
all_emu:
	make clean
	make update
	make $(TARGET) "VERBOSE :=" "emu := 1"
	make cleanup
	
#$(TARGET):  main.f90 fortran.o $(CUDA_OBJ)
$(TARGET):  $(FORTRAN_OBJ) fortran.o $(CUDA_OBJ)
	@ echo "Final Link phase..."
	@ echo "Calling g95..."
	$(FC) $(FFLAGS) -o $(TARGET) $(FORTRAN_OBJ) fortran.o $(CUDA_OBJ) $(LIB) $(CUDA_LIBS) $(CLI)

$(CUDA_OBJ): $(CUFILES_C) $(CUFILES_CPP)
	@ echo "Calling NVCC..."
	$(VERBOSE)$(NVCC) $(NVCCFLAGS) $(CUDA_NVCC_FLAGS) $(SM_xx) -c $(CUFILES_CPP) $(CUFILES_C) $(CLI)

$(FORTRAN_OBJ): $(FORTRAN_FILES)
	@ echo "Calling g95..."
	$(FC) -c $(FFLAGS) $(FORTRAN_FILES)
	
fortran.o: fortran.c
	@ echo "Calling gcc..." 
	gcc -c $(MY_CFLAGS) -DCUBLAS_USE_THUNKING -I$(MY_CUDA_HOME)/include fortran.c $(CLI)

clean:
	rm -f $(TARGET)
	make cleanup
	rm -f logs/*
	
cleanup:
	rm -f *.mod *.o *.mod *.linkinfo *.cu *.C *.c *.f90 *.h
	
update:
	cp src/*.C src/*.c src/*.h src/*.f90 .
	rename C cu	
	rename c cu
	mv fortran.cu fortran.c
	

clean_docs:
	rm -rf docs/*
